import React, {FC, useCallback} from 'react';
import {
  View,
  Text,
  StyleSheet,
  TouchableOpacity,
  Image,
  useWindowDimensions,
  LayoutAnimation,
} from 'react-native';
import {observer} from 'mobx-react';
import HomeStore from '../../../../stores/HomeStore';

import icon_delete from '../../../../assets/icon_delete.png';
import icon_arrow from '../../../../assets/icon_arrow.png';
import {save} from '../../../../utils/Storage';
import {CATEGORY_LIST} from '../../../../constants/common';
type PropsType = {
  hide: () => void;
};
const MyList: FC<PropsType> = observer((props: PropsType) => {
  const {hide} = props;
  const myList = HomeStore.categoryList.filter(item => item.isAdd);
  const onItemPress = useCallback((item: Category) => {
    return () => {
      if (!HomeStore.edit) {
        return;
      }
      LayoutAnimation.spring();
      HomeStore.removeCategoryFromMyList(item);
    };
  }, []);

  const onEditPress = () => {
    if (!HomeStore.edit) {
      save(CATEGORY_LIST, JSON.stringify(HomeStore.categoryList));
    }
    HomeStore.setEdit(!HomeStore.edit);
  };

  const {width: SCREEN_WIDTH} = useWindowDimensions();
  const itemLayoutStyle = StyleSheet.create({
    itemLayout: {
      width: (SCREEN_WIDTH - 80) / 4,
      height: 32,
      justifyContent: 'center',
      alignItems: 'center',
      borderWidth: 1,
      borderColor: '#eee',
      borderRadius: 6,
      marginLeft: 16,
      marginTop: 12,
    },
    itemDefaultLayout: {
      width: (SCREEN_WIDTH - 80) / 4,
      height: 32,
      justifyContent: 'center',
      alignItems: 'center',
      backgroundColor: '#eee',
      borderRadius: 6,
      marginLeft: 16,
      marginTop: 12,
    },
  });
  return (
    <>
      <View style={styles.row}>
        <Text style={styles.titleTxt}>我的频道</Text>
        <Text style={styles.subTitleTxt}>点击进入频道</Text>
        <TouchableOpacity style={styles.editBtn} onPress={onEditPress}>
          <Text style={styles.editTxt}>
            {HomeStore.edit ? '完成编辑' : '进入编辑'}
          </Text>
        </TouchableOpacity>
        <TouchableOpacity style={styles.closeBtn} onPress={hide}>
          <Image source={icon_arrow} style={styles.img} />
        </TouchableOpacity>
      </View>
      <View style={styles.listContent}>
        {myList.map(item => {
          return (
            <TouchableOpacity
              key={item.name}
              style={
                item.default
                  ? itemLayoutStyle.itemDefaultLayout
                  : itemLayoutStyle.itemLayout
              }
              onPress={onItemPress(item)}>
              <Text style={styles.itemTxt}>{item.name}</Text>
              {HomeStore.edit && !item.default && (
                <Image style={styles.itemImage} source={icon_delete} />
              )}
            </TouchableOpacity>
          );
        })}
      </View>
    </>
  );
});

const styles = StyleSheet.create({
  row: {
    width: '100%',
    flexDirection: 'row',
    backgroundColor: 'white',
    alignItems: 'center',
    paddingHorizontal: 16,
  },
  titleTxt: {
    fontSize: 16,
    color: '#333',
    fontWeight: 'bold',
  },
  subTitleTxt: {
    fontSize: 12,
    color: '#999',
    marginStart: 12,
    flex: 1,
  },
  editBtn: {
    paddingHorizontal: 10,
    height: 24,
    backgroundColor: '#eee',
    borderRadius: 12,
    justifyContent: 'center',
    alignItems: 'center',
  },
  editTxt: {
    fontSize: 14,
    color: '#3050ff',
  },
  closeBtn: {
    paddingLeft: 12,
  },
  img: {
    width: 16,
    height: 16,
    resizeMode: 'contain',
    transform: [{rotate: '90deg'}],
  },
  listContent: {
    width: '100%',
    flexDirection: 'row',
    flexWrap: 'wrap',
  },
  itemTxt: {
    fontSize: 16,
    color: '#666',
  },
  itemImage: {
    width: 14,
    height: 14,
    resizeMode: 'contain',
    position: 'absolute',
    top: -6,
    right: -6,
  },
});
export default MyList;
